﻿using System;
using System.Xml;
using System.Xml.Linq;
using unTroublemaker.Parsing;

namespace unTroublemaker
{
    public static class XElementExtension
    {
        /// <summary>Throws parse exception if element has wrong name</summary>
        public static void AssertName(this XElement element, string name)
        {
            if (!element.HasName(name))
                throw new ParseException("Element <" + element.Name.LocalName + "> is not expected " + "<" + name +
                                         "> at line " + element.LineNo());
        }

        public static bool HasName(this XElement element, string name)
        {
            return element.Name.LocalName.Equals(name, StringComparison.CurrentCultureIgnoreCase);
        }

        public static int LineNo(this XElement element)
        {
            var lineNoInfo = element as IXmlLineInfo;
            if (lineNoInfo != null)
                return lineNoInfo.LineNumber;

            return 0;
        }
    }
}
